<template>
  <div class="users">
    <Breadcrumb
      :params1="'用户管理'"
      :params2="'用户列表'"
    ></Breadcrumb>
    <el-card class="box-card">
      <el-row :gutter="20">
        <el-col :span="8"
          ><div class="grid-content bg-purple">
            <el-input
              clearable
              placeholder="请输入内容"
              v-model="input3"
              class="input-with-select"
            >
              <el-button slot="append" icon="el-icon-search"></el-button>
            </el-input></div
        ></el-col>
        <el-col :span="12"
          ><div class="grid-content bg-purple">
            <el-button type="primary" @click="dialogVisibleAdd=true">添加用户</el-button>
          </div>
        </el-col>
      </el-row>

      <el-table :data="userlist" border style="width: 100%">
        <el-table-column type="index" :index="indexMethod" label="#">
        </el-table-column>
        <el-table-column prop="username" label="用户名"> </el-table-column>
        <el-table-column prop="email" label="邮箱"> </el-table-column>
        <el-table-column prop="mobile" label="电话"> </el-table-column>
        <el-table-column prop="role_name" label="角色"> </el-table-column>
        <el-table-column label="状态">
          <template slot-scope="scope">
            <el-switch
              @change="status_hange(scope.row)"
              v-model="scope.row.mg_state"
              active-color="#13ce66"
              inactive-color="#ff4949"
            >
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="180">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="primary"
              icon="el-icon-edit"
              @click="edit(scope.row)"
            ></el-button>
            <el-button
              size="mini"
              type="danger"
              icon="el-icon-delete"
              @click="del(scope.row.id)"
            ></el-button>
            <el-button
              size="mini"
              type="info"
              icon="el-icon-setting"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="params.pagenum"
        :page-sizes="[4, 6, 8, 10]"
        :page-size="params.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
    <el-dialog title="修改用户" :visible.sync="dialogFormVisible">
      <el-form
        :model="form"
        :rules="rulesEdit"
        ref="editForm"
       
      >
        <el-form-item label="用户名" :label-width="formLabelWidth">
          <el-input
            disabled
            v-model="form.username"
            auto-complete="off"
          ></el-input>
        </el-form-item>

        <el-form-item prop="email" label="邮箱" :label-width="formLabelWidth">
          <el-input v-model="form.email" auto-complete="off"></el-input>
        </el-form-item>

        <el-form-item prop="mobile" label="手机" :label-width="formLabelWidth">
          <el-input v-model="form.mobile" auto-complete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="affirm_edit">确 定</el-button>
      </div>
    </el-dialog>
    <el-dialog
      title="添加用户"
      :visible.sync="dialogVisibleAdd"
     
      :before-close="handleClose"
    >
      <el-form
        :model="addform"
        :rules="rulesEdit"
        ref="addform"
       
      >
        <el-form-item prop="username" label="用户名" :label-width="formLabelWidth">
          <el-input
           
            v-model="addform.username"
            auto-complete="off"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password" label="密码" :label-width="formLabelWidth">
          <el-input
           
            v-model="addform.password"
            auto-complete="off"
          ></el-input>
        </el-form-item>

        <el-form-item prop="email" label="邮箱" :label-width="formLabelWidth">
          <el-input v-model="addform.email" auto-complete="off"></el-input>
        </el-form-item>

        <el-form-item prop="mobile" label="手机" :label-width="formLabelWidth">
          <el-input v-model="addform.mobile" auto-complete="off"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisibleAdd = false">取 消</el-button>
        <el-button type="primary" @click="useradd"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  getusers_api,
  chandestatus_api,
  deluser_api,
  edituser_api,
  adduser_api
} from '@/api/index'
export default {
  data() {
    return {
      userlist: [],
      input3: '',
      currentPage1: 5,
      total: 1,
      params: {
        query: '',
        pagenum: 1,
        pagesize: 4,
      },
      breadroute:['用户管理', '用户列表'],
      dialogFormVisible: false,
      dialogVisibleAdd : false,
      formLabelWidth: '200',
      form: {
        email: '',
        mobile: '',
        id: '',
        username: '',
      },
      addform:{
        email: '',
        mobile: '',
        password: '',
        username: '',
      },
      rulesEdit: {
        email: [
          { required: true, message: '请输入邮箱', trigger: 'blur' },
          { min: 5, max: 20, message: '长度在 5 到 20 个字符', trigger: 'blur' },
        ],
        mobile: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
          { min: 11, max: 11, message: '长度在 11个字符', trigger: 'blur' },
        ],
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 6, message: '长度在3 到 6个字符', trigger: 'blur' },
        ],
         password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 3, max: 6, message: '长度在3 到 6个字符', trigger: 'blur' },
        ],
      },
    }
  },
  methods: {
    indexMethod(index) {
      return index * 1
    },

    async getUsers() {
      var res = await getusers_api({
        params: this.params,
      })
      // console.log(res)

      if (res.data.meta.status == 200) {
        this.$message.success(res.data.meta.msg)
        this.userlist = res.data.data.users
        this.total = res.data.data.total
        // console.log(this.userlist)
      } else {
        this.$message.error(res.data.meta.msg)
      }
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`)
      this.params.pagesize = val
      this.getUsers()
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`)
      this.params.pagenum = val
      this.getUsers()
    },
    async status_hange(row) {
      //  console.log(row);

      var res = await chandestatus_api({
        uId: row.id,
        type: row.mg_state,
      })
      // console.log(res);
      if (res.data.meta.status == 200) {
        this.$message.success(res.data.meta.msg)

        // console.log(this.userlist)
      } else {
        this.$message.error(res.data.meta.msg)
      }
    },
    del(id) {
      this.$confirm('从数据库删除该用户, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(async () => {
          var res = await deluser_api(id)
          console.log(res)
          if (res.data.meta.status == 200) {
            this.$message({
              type: 'success',
              message: res.data.meta.msg,
            })
            this.getUsers()
          } else {
            this.$message.error(res.data.meta.msg)
          }
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除',
          })
        })
    },
    edit(row) {
      this.dialogFormVisible = true
      this.form.email = row.email
      this.form.mobile = row.mobile
      this.form.username = row.username
      this.form.id = row.id
    },

    // 确认编辑
    affirm_edit() {
      this.$refs.editForm.validate(async (valid) => {
        if (valid) {
          // alert('submit!');
          console.log(this.form)
          var res = await edituser_api(this.form)
          if (res.data.meta.status == 200) {
            this.$message({
              type: 'success',
              message: res.data.meta.msg,
            })
            this.getUsers()
          } else {
            this.$message.error(res.data.meta.msg)
          }
          this.dialogFormVisible = false
        } else {
          this.$message.error('表单检验不成功！！！')
          return false
        }
      })
    },


 // 添加用户函数
    useradd() {
       
      this.$refs.addform.validate(async (valid) => {
        if (valid) {
          // alert('submit!');
          // console.log(this.addform)
          var res = await adduser_api(this.addform)
          if (res.data.meta.status == 201) {
            this.$message({
              type: 'success',
              message: res.data.meta.msg,
            })
            this.getUsers()
          } else {
            this.$message.error(res.data.meta.msg)
          }
        this.dialogVisibleAdd = false
        } else {
          this.$message.error('表单检验不成功！！！')
          return false
        }
      })
    },
    handleClose(done) {
      this.$confirm('确认关闭？')
        .then((_) => {
          done()
        })
        .catch((_) => {})
    },

   

  
  },
  mounted() {
    this.getUsers()
  },
}
</script>

<style scoped lang='less'>
.box-card {
  margin: 0 auto;
  width: 95%;
}
.el-row {
  margin: 15px 0;
}
.el-form {
  margin: 0 auto;
}
</style>